<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="审核" prop="orderRetreatStatus">
        <el-select v-model="queryParams.orderRetreatStatus" placeholder="请选择0未审核，1审核通过，2审核未通过" clearable size="small">
          <el-option label="未审核" value="0" />
          <el-option label="审核通过" value="1" />
          <el-option label="审核未通过" value="2" />
        </el-select>
      </el-form-item>



      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:retreat:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:retreat:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:retreat:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:retreat:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="retreatList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="退货表的id" align="center" prop="orderRetreatId" />
      <el-table-column label="订单表的id" align="center" prop="orderId" />
      <el-table-column label="退货人的id" align="center" prop="customerId" />
      <el-table-column label="退货的原因" align="center" prop="orderRetreatReason" />
      <el-table-column label="退货的申请时间" align="center" prop="orderRetreatApplicationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderRetreatApplicationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="退货的审核时间" align="center" prop="orderRetreatAuditTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.orderRetreatAuditTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="审核" align="center" prop="orderRetreatStatus" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderRetreatStatus == '0'">正在退款</el-tag>
          <el-tag v-else-if="scope.row.orderRetreatStatus == '1'" type="success">退款成功</el-tag>
          <el-tag v-else-if="scope.row.orderRetreatStatus == '2'" type="danger">退款失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核未通过的原因" align="center" prop="orderRetreatFailedReason" />
      <el-table-column label="商家的id" align="center" prop="supplierId" />
      <el-table-column label="退货的凭证图片" align="center" prop="orderRetreatPicture">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-picture-outline" @click="showPicture(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="退货凭证图片" :visible.sync="openStatus" width="60%">
      <div style="display: flex;margin: 0 auto">
        <div v-for="item in pictureList">
          <el-image :src="item.orderPictureUrl" :preview-src-list="[item.orderPictureUrl]" style="margin-left: 20px;width: 100px; height: 100px;display: flex;" />
        </div>
      </div>

    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改申请退货对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">


<!--        <el-form-item label="退货的申请时间" prop="orderRetreatApplicationTime">-->
<!--          <el-date-picker clearable size="small" style="width: 200px"-->
<!--                          v-model="form.orderRetreatApplicationTime"-->
<!--                          type="date"-->
<!--                          value-format="yyyy-MM-dd"-->
<!--                          placeholder="选择退货的申请时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->

        <el-form-item label="审核">
<!--          <el-radio-group v-model="form.orderRetreatStatus">-->
<!--            <el-radio label="1">请选择字典生成</el-radio>-->
<!--          </el-radio-group>-->
          <el-select v-model="form.orderRetreatStatus" clearable size="small">
            <el-option label="未审核" value="0" />
            <el-option label="审核通过" value="1" />
            <el-option label="审核未通过" value="2" />
          </el-select>
        </el-form-item>


        <el-form-item label="审核未通过的原因" prop="orderRetreatFailedReason">
          <el-input v-model="form.orderRetreatFailedReason" placeholder="请输入审核未通过的原因" />
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { listAfterSale, getAfterSale, delAfterSale, addAfterSale, updateAfterSale, exportAfterSale, updateRefund } from "@/api/shopping/afterSale";
import { listRetreat,getRetreat,updateRetreat } from "@/api/retreat";

export default {
  name: "AfterSale",
  data() {
    return {
      pictureList: [],
      openStatus: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 申请退货表格数据
      retreatList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        customerId: null,
        orderRetreatReason: null,
        orderRetreatPicture: null,
        orderRetreatApplicationTime: null,
        orderRetreatAuditTime: null,
        orderRetreatStatus: null,
        orderRetreatFailedReason: null,
        supplierId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "订单表的id不能为空", trigger: "blur" }
        ],
        customerId: [
          { required: true, message: "退货人的id不能为空", trigger: "blur" }
        ],
        orderRetreatReason: [
          { required: true, message: "退货的原因不能为空", trigger: "blur" }
        ],
        orderRetreatPicture: [
          { required: true, message: "退货的凭证图片不能为空", trigger: "blur" }
        ],
        orderRetreatApplicationTime: [
          { required: true, message: "退货的申请时间不能为空", trigger: "blur" }
        ],
        orderRetreatStatus: [
          { required: true, message: "0未审核，1审核通过，2审核未通过不能为空", trigger: "blur" }
        ],
        supplierId: [
          { required: true, message: "商家的id不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    showPicture(row){
      this.pictureList = row.list;
      this.openStatus = true;
    },
    /** 查询申请退货列表 */
    getList() {
      this.loading = true;
      listRetreat(this.queryParams).then(response => {
        this.retreatList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderRetreatId: null,
        orderId: null,
        customerId: null,
        orderRetreatReason: null,
        orderRetreatPicture: null,
        orderRetreatApplicationTime: null,
        orderRetreatAuditTime: null,
        orderRetreatStatus: 0,
        orderRetreatFailedReason: null,
        supplierId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderRetreatId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加申请退货";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderRetreatId = row.orderRetreatId || this.ids

      getRetreat(orderRetreatId).then(response => {
        console.log("***"+JSON.stringify(response.data))
        this.form = response.data;
        this.open = true;
        this.title = "修改申请退货";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderRetreatId != null) {
            updateRetreat(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addRetreat(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderRetreatIds = row.orderRetreatId || this.ids;
      this.$confirm('是否确认删除申请退货编号为"' + orderRetreatIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delRetreat(orderRetreatIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有申请退货数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportRetreat(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function() {});
    }
  }
};
</script>
